﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyTools
{
    public partial class Form1 : Form
    {
        private string connectionString = "server=10.201.20.30;uid=DB1127;pwd=H7RVMdc74t;database=PPMS;Connect Timeout=120;";//本地服务器
        public Form1()
        {
            InitializeComponent();
            this.butDelete.Enabled = false;
        }

        private void butQuery_Click(object sender, EventArgs e)
        {


            if (txtNumericalorder.Text.Trim() != "")
            {
                if (ProcessSqlStr(txtNumericalorder.Text.Trim()))
                {
                    var numericalorder = 0L;
                    try
                    {
                        numericalorder = long.Parse(txtNumericalorder.Text.Trim());

                    }
                    catch { }
                    if (numericalorder == 0L)
                    {
                        MessageBox.Show("流水号错误"); return;
                    }


                    DataSet ds = GetFDSettleReceipt(numericalorder);
                    //使用DataSet绑定时，必须同时指明DateMember 
                    this.dataGridView1.DataSource = ds;
                    this.dataGridView1.DataMember = "ds";

                    this.butDelete.Enabled = true;

                }
            }

        }





        public bool ProcessSqlStr(string inputString)
        {
            string SqlStr = @"and|or|exec|execute|insert|select|delete|update|alter|create|drop|count|\*|chr|char|asc|mid|substring|master|truncate|declare|xp_cmdshell|restore|backup|net +user|net +localgroup +administrators";
            try
            {
                if ((inputString != null) && (inputString != String.Empty))
                {
                    string str_Regex = @"\b(" + SqlStr + @")\b";

                    Regex Regex = new Regex(str_Regex, RegexOptions.IgnoreCase);
                    //string s = Regex.Match(inputString).Value; 
                    if (true == Regex.IsMatch(inputString))
                        return false;

                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            if (txtNumericalorder.Text.Trim() != "")
            {
                if (ProcessSqlStr(txtNumericalorder.Text.Trim()))
                {
                    if (MessageBox.Show("是否确认删除该单据" + txtNumericalorder.Text.Trim() + "？", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        this.butDelete.Enabled = false;

                        if (txtNumericalorder.Text.Trim() != "")
                        {
                            if (ProcessSqlStr(txtNumericalorder.Text.Trim()))
                            {
                                var numericalorder = 0L;
                                try
                                {
                                    numericalorder = long.Parse(txtNumericalorder.Text.Trim());
                                }
                                catch { }

                                if (numericalorder == 0L)
                                {
                                    MessageBox.Show("流水号错误"); return;
                                }

                                DeleteFDSettleReceipt(numericalorder);
                                DataSet ds = GetFDSettleReceipt(numericalorder);
                                //使用DataSet绑定时，必须同时指明DateMember 
                                this.dataGridView1.DataSource = ds;
                                this.dataGridView1.DataMember = "ds";
                            }
                        }


                    }
                }
            }

        }


        /// <summary>
        /// 读取配方编码表和商品信息表
        /// </summary>
        /// <returns></returns>
        public DataSet GetFDSettleReceipt(long numericalorder)
        {
            DataSet ds = new DataSet();
            try
            {
                string StrSql = string.Format(@" select iNumericalOrder as '流水号' , nDebit  as '借方金额' , nCredit as '贷方金额' from FDSettleReceiptItem where inumericalorder= " + numericalorder + ";");
                ds = DBManage.Query(StrSql, connectionString);
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误提示：GetFormula_Product方法异常：" + ex.Message);
            }
            return ds;
        }
        /// <summary>
        /// 读取配方编码表和商品信息表
        /// </summary>
        /// <returns></returns>
        public int DeleteFDSettleReceipt(long numericalorder)
        {
            var result = 0;
            try
            {
                string StrSql = string.Format(@" delete  from FDSettleReceipt where inumericalorder= {0};
                                                 delete  from FDSettleReceiptItem where inumericalorder=  {0};", numericalorder);
                result = DBManage.ExecuteSql(StrSql, connectionString);
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误提示：GetFormula_Product方法异常：" + ex.Message);
            }
            return result;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // GoogleMapServices map = new GoogleMapServices();
            //MessageBox.Show( map.GetLatLngByAddr(textmapAdder.Text.Trim()).ToString());

            decimal a = Convert.ToDecimal(textmapAdder.Text.Trim());

            var at = Math.Floor(Math.Floor(1000M / 10M) *0.1M / 1.22M) * 1.22M;

            var at1 = Math.Floor(1000M / 10M * 0.1M / 1.22M) * 1.22M;
            MessageBox.Show(at.ToString());
        }
    }
}
